package com.itheima.service.imp;

import com.itheima.common.PageBean;
import com.itheima.dto.StudentDto;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import com.itheima.vo.StudentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;
    @Override
    public PageBean<StudentVo> pageQuery(StudentDto student) {
        PageBean<StudentVo> pageBean = new PageBean<>();
        student.setPage((student.getPage()-1)*student.getPageSize());
        pageBean.setRows(studentMapper.pageQuery(student));
        pageBean.setTotal(studentMapper.countQuery(student));
        return pageBean;
    }
    public void test(){
        log.info("测试");
    }

    @Override
    public void insert(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount(0);
        student.setViolationScore(0);
        studentMapper.insert(student);
    }

    @Override
    public Student selectById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void vo(Integer id, Integer score) {
        Student student = selectById(id);
        student.setViolationScore(student.getViolationScore()+score);
        student.setViolationCount(student.getViolationCount()+1);
        student.setUpdateTime(LocalDateTime.now());
        update(student);
    }

    @Override
    public void delete(Integer id) {
        studentMapper.deleteById(id);
    }

    @Override
    public void deletes(List<Integer> ids) {
        for(Integer id : ids) {
            studentMapper.deleteById(id);
        }
    }
}
